﻿using System.Windows;
using System.Windows.Media;

namespace VisualView
{
    /// <summary>
    /// Grometry显示控件
    /// </summary>
    public class GeometryView : DrawingVisualView
    {
        static GeometryView()
        {
            DefaultPen.Freeze();
        }

        public GeometryView()
        {
            base.Visual = new DrawingVisual();
        }

        /// <summary>
        /// 填充色
        /// </summary>
        public Brush FillBrush
        {
            get { return (Brush)GetValue(FillBrushProperty); }
            set { SetValue(FillBrushProperty, value); }
        }
        public static readonly DependencyProperty FillBrushProperty = DependencyProperty.Register("FillBrush", typeof(Brush), typeof(GeometryView), new PropertyMetadata(Brushes.Transparent, PropertyChangedNeedRefresh));
        internal static Pen DefaultPen = new Pen(Brushes.Black, 1);
        /// <summary>
        /// 边框画笔
        /// </summary>
        public Pen StrokePen
        {
            get { return (Pen)GetValue(StrokePenProperty); }
            set { SetValue(StrokePenProperty, value); }
        }
        public static readonly DependencyProperty StrokePenProperty = DependencyProperty.Register("StrokePen", typeof(Pen), typeof(GeometryView), new PropertyMetadata(DefaultPen, PropertyChangedNeedRefresh));

        /// <summary>
        /// 需要显示的Geometry
        /// </summary>
        public Geometry Geometry
        {
            get { return (Geometry)GetValue(GeometryProperty); }
            set { SetValue(GeometryProperty, value); }
        }
        public static readonly DependencyProperty GeometryProperty = DependencyProperty.Register("Geometry", typeof(Geometry), typeof(GeometryView), new PropertyMetadata(PropertyChangedNeedRefresh));
        protected static void PropertyChangedNeedRefresh(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is GeometryView sv) sv.Refresh();
        }

        /// <summary>
        /// 刷新显示内容到页面
        /// </summary>
        public virtual void Refresh()
        {
            using (var dc = Visual.RenderOpen())
            {
                if (Geometry != null) dc.DrawGeometry(FillBrush, StrokePen, Geometry);
            }
        }
    }
}
